package com.ssms.controller;

import com.ssms.commons.ESQueryParam;
import com.ssms.commons.Result;
import com.ssms.commons.TablePage;
import com.ssms.model.Exam;
import com.ssms.model.ExamScore;
import com.ssms.model.SysUser;
import com.ssms.service.ExamScoreService;
import com.ssms.service.ExamService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

@Controller
@RequestMapping("/es")
public class ExamScoreController {

    @Resource
    private ExamScoreService examScoreService;

    @RequestMapping()
    public String exam() {
        return "examScore";
    }

    @RequestMapping("list")
    @ResponseBody
    public TablePage list(TablePage tablePage, ESQueryParam esQueryParam) {
        return examScoreService.list(tablePage, esQueryParam);
    }

    @RequestMapping("query")
    @ResponseBody
    public Result query(Integer stuId) {
        return examScoreService.query(stuId);
    }

    @RequestMapping("export")
    public void getStuScore(Integer stuId, HttpServletResponse response) throws UnsupportedEncodingException {
        examScoreService.createFile(stuId,response);
    }

//    @RequestMapping("add")
//    @ResponseBody
//    public Result add(Exam exam) {
//        return examScoreService.save(exam);
//    }

    @RequestMapping("import")
    @ResponseBody
    public Result imports(@RequestParam("file") MultipartFile file) throws IOException {
        return examScoreService.save(file);
    }

    @RequestMapping("update")
    @ResponseBody
    public Result update(ExamScore examScore, HttpSession session) {
        SysUser user = (SysUser) session.getAttribute("sysUser");
        Integer instor = null;
        if(user.getRoleType() == 2){
            instor = user.getId();
        }
        return examScoreService.update(examScore,instor);
    }

    @RequestMapping("delete")
    @ResponseBody
    public Result delete(ExamScore examScore, HttpSession session) {
        SysUser user = (SysUser) session.getAttribute("sysUser");
        Integer instor = null;
        if(user.getRoleType() == 2){
            instor = user.getId();
        }
        return examScoreService.delete(examScore,instor);
    }

}
